package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.core.utils.JsonUtils;
import com.huaweicloud.sdk.koomessage.v1.KooMessageClient;
import com.huaweicloud.sdk.koomessage.v1.model.CreateVmsSendTaskRequest;
import com.huaweicloud.sdk.koomessage.v1.model.CreateVmsSendTaskRequestBody;
import com.huaweicloud.sdk.koomessage.v1.model.CreateVmsSendTaskResponse;
import com.huaweicloud.sdk.koomessage.v1.model.ListVmsSendTasksRequest;
import com.huaweicloud.sdk.koomessage.v1.model.ListVmsSendTasksResponse;
import com.huaweicloud.sdk.koomessage.v1.model.ListVmsTemplateStatusRequest;
import com.huaweicloud.sdk.koomessage.v1.model.ListVmsTemplateStatusResponse;
import com.huaweicloud.sdk.koomessage.v1.model.VmsTemplateStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class SendVmsTask {
    private static final Logger logger = LoggerFactory.getLogger(SendVmsTask.class);

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        BasicCredentials credentials = new BasicCredentials().withAk(ak).withSk(sk);

        HttpConfig config = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true);
        KooMessageClient kmClient = KooMessageClient.newBuilder()
            .withCredential(credentials)
            .withEndpoint("koomessage.myhuaweicloud.com")
            .withHttpConfig(config)
            .build();

        // 您可以通过KooMessage控制台查询到以下参数
        // 智能信息基础版模板ID,KooMessage控制台-》智能信息基础版-》模板管理-》模板ID
        String tplId = "{YOUR tplId}";
        // 查询模板状态
        VmsTemplateStatus template = listVmsTemplateStatus(kmClient, tplId);
        // audit_state为0，即审核通过
        if (template != null && template.getAuditState() == 0) {
            // 创建智能信息基础版发送任务
            CreateVmsSendTaskResponse sendVmsTaskResponse = createVmsSendTask(kmClient, tplId);
            System.out.println(JsonUtils.toJSON(sendVmsTaskResponse));
        }

        // 查询智能信息基础版发送任务
        ListVmsSendTasksResponse response = listVmsSendTasks(kmClient);
        System.out.println(JsonUtils.toJSON(response));

    }

    /**
     * 查询智能信息基础版模板
     *
     * @param kmClient 请求客户端
     * @param tplId    模板id
     */
    public static VmsTemplateStatus listVmsTemplateStatus(KooMessageClient kmClient, String tplId) {
        try {
            ListVmsTemplateStatusRequest request = new ListVmsTemplateStatusRequest();
            request.setTplId(tplId);
            request.setLimit(10);
            request.setOffset(0);
            ListVmsTemplateStatusResponse response = kmClient.listVmsTemplateStatus(request);
            if (response != null) {
                return response.getData().getTemplates().get(0);
            }
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return null;
    }

    /**
     * 创建智能信息基础版发送任务
     *
     * @param kmClient 请求客户端
     */
    public static CreateVmsSendTaskResponse createVmsSendTask(KooMessageClient kmClient, String tplId) {
        try {
            CreateVmsSendTaskRequest request = new CreateVmsSendTaskRequest();
            CreateVmsSendTaskRequestBody vmsSendBody = new CreateVmsSendTaskRequestBody();
            vmsSendBody.setTaskName("{YOUR TASK NAME}");
            vmsSendBody.setExpirationTime(72);
            // tplId可通过模板列表获取，使用已审核通过的模板id
            vmsSendBody.setTplId(tplId);
            List<String> mobiles = new ArrayList<>();
            // 替换为要发送的手机号码
            mobiles.add("{YOUR MOBILE}");
            vmsSendBody.setMobiles(mobiles);
            request.setBody(vmsSendBody);
            return kmClient.createVmsSendTask(request);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return null;
    }

    /**
     * 查询智能信息基础版发送任务
     *
     * @param kmClient 请求客户端
     */
    public static ListVmsSendTasksResponse listVmsSendTasks(KooMessageClient kmClient) {
        try {
            ListVmsSendTasksRequest request = new ListVmsSendTasksRequest();
            return kmClient.listVmsSendTasks(request);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return null;
    }
}
